// CICP by Arkowitz

string cicp_host;
integer config_line = 0;
key cardKey;
list http_list = [];

integer PRIM_COUNT = 0;
integer sleepChunk = 100;
key curReq;
integer sheepMode = 0;
integer gotPong = 0;
float stepDist = .012;
float gridSide = 16;

integer curPrim = 1;
vector rezPos;
vector origPos;
integer xinc = 0;
integer yinc = 0;
integer zinc = 0;

rezSheep () {
    sheepMode = 1;
    curPrim = 1;
    if ( llGetSubString(cicp_host, 0, 4) == "https" ) {
        http_list = [HTTP_VERIFY_CERT, FALSE];
        llOwnerSay("using ssl");
    }
    
    llOwnerSay("rezzing " + (string)PRIM_COUNT + " sheep, boss");
    rezPos = llGetPos();
    origPos = rezPos;
    origPos.x -= gridSide * stepDist / 2;
    origPos.y -= gridSide * stepDist / 2;
    origPos.z -= gridSide * stepDist / 2;
    rezNextSheep();
}
    
rezNextSheep () {
    if ( curPrim <= PRIM_COUNT ) {
        rezPos.x = origPos.x + xinc * stepDist;
        rezPos.y = origPos.y + yinc * stepDist;
        rezPos.z = origPos.z + zinc * stepDist;
        if ( xinc < gridSide )
            xinc++;
        else {
            xinc = 0;
            if ( yinc < gridSide )
                yinc++;
            else {
                yinc = 0;
                zinc++;
            }
        }
        if ( curPrim % sleepChunk == 0 ) {
            //llSleep(sleepTime);
            llOwnerSay((string)curPrim + " of " + (string)PRIM_COUNT +
                       " sheep rezzed, boss");
        }
        llRezObject("cicp_prim", rezPos,
                    <0,0,0>, <0,0,0,0>, curPrim);
    }
    else {
        llOwnerSay("all " + (string)PRIM_COUNT + " sheep rezzed, boss");
        sheepMode = 2;
        curReq = llHTTPRequest(cicp_host + "type=initregion&region=" +
                              llGetRegionName() + "&primcount=" +
                               (string)PRIM_COUNT, http_list, "");
    }
}

default {

    state_entry () {
        llOwnerSay( "CICP Prim Rezzer" );
        llListen(0, "", llGetOwner(), "");
        llListen(-999999, "", "", "");
    }

    touch_start ( integer total_number ) {
        if ( sheepMode == 1 )
            llOwnerSay("Not done rezzing sheep!");
        else {
            gotPong = 0;
            llOwnerSay("checking for sheep...");
            llRegionSay(-1, "ping");
            llSetTimerEvent(2);
        }
    }

    listen ( integer channel, string name, key id, string message ) {
        if ( channel == -999999 ) {
            if ( message == "pong" && sheepMode != 1 ) {
                gotPong = 1;
                sheepMode = 2;
                vector startRezPos = llGetPos();
                startRezPos.z += 1;
                llRezObject("cicp_start", startRezPos, <0,0,0>, <0,0,0,0>, 0);
            }
        }
        else if ( channel == 0 ) {
            if ( message == "totem_allreset" ) {
                llOwnerSay("sending reset to servlet, boss");
                curReq = llHTTPRequest(cicp_host + "type=initregion&region=" +
                                       llGetRegionName() + "&primcount=" +                                                    (string)PRIM_COUNT, http_list, "");                
            }
        }
    }
            
    http_response ( key request_id, integer status, list metadata,
string body ) {
    }
    
    dataserver ( key queryId, string data ) {
        if ( queryId == cardKey ) {
            if ( config_line == 0 ) {
                cicp_host = data;
                config_line++;
                cardKey = llGetNotecardLine("cicp_config", config_line);
            }
            else if ( config_line == 1 ) {
                PRIM_COUNT = (integer)data;
                rezSheep();
            }
        }
    }
    
    object_rez ( key id ) {
        llGiveInventory(id, "cicp_config");
        if ( sheepMode == 1 ) {
            curPrim++;
            rezNextSheep();
        }
    }
    
    timer () {
        llSetTimerEvent(0);
        if ( gotPong == 0 )
            cardKey = llGetNotecardLine("cicp_config", config_line);
    }
}
